<?php
class Mysql
{
    private $db_host = "localhost:3306";
    private $db_user = "root";
    private $db_pwd = "root";
    private $db_name = "nanyuedb";
    private $db_charset = "utf-8";
    public function insert ($table, $data)
    {
        $fields = array_keys($data);
        $values = array_values($data);
        $query = "INSERT INTO $table ( " . implode(" , ", $fields) . " ) VALUES ( '" . implode("' , '", $values) . "' )";
        return $this->query($query);
    }
    public function update ($table, $data, $oid)
    {
        $sets = array();
        foreach ($data as $keys => $values) {
            $sets[] = $keys . " = '" . $values . "'";
        }
        $query = "UPDATE $table SET " . implode(' , ', $sets) . " WHERE oid = " . $oid;
        return $this->query($query);
    }
    public function delete ($table, $where)
    {
        $query = "DELETE FROM $table WHERE " . $where;
        return $this->query($query);
    }
    public function query ($query)
    {
        $this->log($query);
        $link = mysql_connect($this->db_host, $this->db_user, $this->db_pwd);
        mysql_select_db($this->db_name);
        mysql_query("SET NAMES 'utf8'", $link);
        return mysql_query($query, $link);
    }
    public function get_results ($query)
    {
        $result = array();
        $rs = $this->query($query);
        while ($row = mysql_fetch_array($rs)) {
            $result[] = $row;
        }
        return $result;
    }
    public function get_row ($query)
    {
        $rs = $this->query($query);
        $result = array();
        if (mysql_num_rows($rs) == 0) {
            return null;
        } else {
            $result = mysql_fetch_array($rs);
            return $result;
        }
    }
    public function log ($sql)
    {
//         $current_date = date("Y-m-d H:i:s");
//         $handle = fopen("debug.txt", 'a');
//         $loginfo = "FrameWork: " . $current_date . "    SQL: " . $sql . "\n";
//         fwrite($handle, $loginfo);
//         fclose($handle);
    }
}
?>